package com.situ.stmall.controller.api;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.situ.stmall.entity.APIResult;
import com.situ.stmall.entity.Shopcart;
import com.situ.stmall.entity.User;
import com.situ.stmall.service.ShopcartService;

@RestController
@RequestMapping("/api/cart")
public class APICartController {

	@Autowired
	private ShopcartService shopcartService;
	
	@GetMapping("/getAll")
	public List getAll(HttpSession session) {
		//获取参数
		User user = (User) session.getAttribute("user");
		//调用
		return shopcartService.getByUserId(user.getId());
		//返回
	}
	
	@GetMapping("/update")
	public APIResult update(Shopcart cart) {
		Shopcart selectCart = shopcartService.update(cart);
		
		if (selectCart.getCount() == cart.getCount()) {
			return APIResult.success(null,selectCart);
		}else {
			return APIResult.fail("库存不足，修改失败",selectCart);
		}
		
//		需要真实数据库数据
//		if (res>0) {
//			return APIResult.ok(null);
//		}else if (res == -1) {
//			APIResult apiResult = new APIResult();
//			apiResult.setStatus(-1);
//			apiResult.setMsg("库存不足");
//			return apiResult;
//		}
//		else {
//			return APIResult.notFound("购物车修改失败");
//		}
	}
	
	@GetMapping("/delete")
	public APIResult remove(Integer id) {
		Shopcart cart = shopcartService.removeById(id);
		if (cart != null) {
			return APIResult.ok(cart);
		} else{
			return APIResult.notFound("未找到目标，删除失败");
		}
		
	}
}
